package cpr.command;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cpr.DefaultCommandHandler;
import cpr.Printer;
import cpr.Rule;
import cpr.Utils;
import cpr.context.Context;
import cpr.context.ruleacceptor.GlobalRuleNrSetRuleAcceptor;
import cpr.data.HLVector;
import cpr.helper.ScoreHelper;
import data.Dictionary;

public class CmdPrintRule extends DefaultCommandHandler {
	public final static int TYPE_GLOBAL = 1;
	public final static int TYPE_SOLVE_CONTEXT= 2;
	
	public CmdPrintRule(String command, int type) {
		super(command,"prints rule: print-rule RULE_NR SCORE [digital]",null,null);
		this.type = type;
	}
	private int type = -1;
	public void execute(String[] params) {
//		Rule.totalRuleNr = 0;
		ScoreHelper helper = getCommandContext().getContext().getScoreHelper();
		int score = helper.convertStringToScore(getParameter(params, 2,"7880704"));
		Context ctx = getCommandContext().getContext();
        
		int ruleId = Integer.parseInt(getParameter(params, 1,"0"));
		Set ruleNr = new HashSet();
		ruleNr.add(Integer.valueOf(ruleId));
		GlobalRuleNrSetRuleAcceptor ruleAcceptor= new GlobalRuleNrSetRuleAcceptor(ruleNr);
		GlobalRuleNrSetRuleAcceptor ruleAcceptorPure= new GlobalRuleNrSetRuleAcceptor(ruleNr);
		String path = getCommandContext().getContext().getDigitalDataFilePath();

        List hl_rules_strings_digs = null;
        List hl_rules_pure = null;
        boolean printStrings = false;
        boolean printDigs = false;
        boolean printPureDigs = true;

        switch (type) {
        case TYPE_GLOBAL:
        	hl_rules_strings_digs = getCommandContext().getContext().getIOContext().readRulesFromHLFile(path, 1,false, ruleAcceptor);
        	hl_rules_pure = getCommandContext().getContext().getIOContext().readRulesFromPureDigitalFile(getCommandContext().getContext().getPureDigitalDataFilePath(), 1,ruleAcceptorPure);
    		
        	printStrings = true;
        	printDigs = true;
        	break;
        case TYPE_SOLVE_CONTEXT:
        	hl_rules_pure = ctx.getCurrentSolveContext().hl_rules;
        	break;
        }
        
		if (printStrings) {
	        Rule aRule = (Rule)hl_rules_strings_digs.get(0);
	        String s = aRule.toString();
	        String s1 = ctx.getPrintContext().printRuleAccordingToVectorFull(aRule, false);
	        Printer.println("Strings:\n" + s1);
		}
		if (printDigs){
	        Rule aRule = (Rule)hl_rules_strings_digs.get(0);
			String s2 = ctx.getPrintContext().printRuleAccordingToVectorFull(aRule, true);
	        Printer.println("Digital:\n" + s2);
		}
		if (printPureDigs) {
			Rule aRule = null;
            aRule = (Rule)hl_rules_pure.get(0);
            String s2 = ctx.getPrintContext().printRuleAccordingToVectorFull(aRule, true);
            Printer.println("Pure Digital:\n" + s2);
	        if (score!=0) {
	        	String rstr = ctx.getPrintContext().printRuleUsingDict(aRule, new HLVector(score));
	        	Printer.println(rstr);	
	        	int hash = Utils.hashCodeForProjection(aRule, new HLVector(score));
	        	Printer.println("hash="+hash);	
	        	
	        	String digest = ctx.getPrintContext().createRuleDigitalDigest(aRule, new HLVector(score),"-");
	        	Printer.println("digest="+digest);	
	        }
		}
	}
    
	
}
